iT邦幫忙

0

AlphaCamp學期三A5作業Callback練習

  • 分享至 

  • xImage
  •  

原始程式碼

const step0 = 'step 0 - start'
const step1 = 'step 1 - run'
const step2 = 'step 2 - run'
const stepDone = 'step Done'

// @TODO:請修改以下函式
const start = (callback) => {
  setTimeout(() => {
    console.log(step0)
    callback()
  }, 500)
}

// @TODO:請修改以下函式
const stepEvent1 = () => {
  return (callback) => {
    console.log(step1)
  }
}

// @TODO:請修改以下函式
const stepEvent2 = () => {
   () => {
    setTimeout(() => {
      console.log(step2)
    }, 500)
  }
}

const stepEventDone = () => {
  return console.log(stepDone)
}

// start(stepEvent1)
stepEvent2()

預期結果

step 0 - start
step 1 - run
step 2 - run
step Done

解題歷程

由內層往外層

由stepEventDone往回推

完成結果如下

const step0 = 'step 0 - start'
const step1 = 'step 1 - run'
const step2 = 'step 2 - run'
const stepDone = 'step Done'

//start(stepEvent1)
const start = (callback) => {
  setTimeout(() => {
    console.log(step0)
    // @TODO:
    callback()(stepEvent2)//stepEvent1()(stepEvent2)
  }, 500)
}

const stepEvent1 = () => {
  return (callback) => {
    // @TODO:
    console.log(step1)
    callback()()//stepEvent2()()
  }
}


const stepEvent2 = () => {
  return () => {
    setTimeout(() => {
      // @TODO
      console.log(step2)
      stepEventDone()
    }, 500)
  }
}
const stepEventDone = () => {
  return console.log(stepDone)
}
// stepEvent2()()
// stepEvent1()(stepEvent2)
start(stepEvent1)

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言